Assignment 4 - Tables

Author

Gabrielle Clary

knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)

Data Source

What I want to convey

Functionality in the table

Formatting in the table

Code

# load data set and packages 
library(dplyr)
library(mapproj)
library(tidyverse)
library(usdata)
library(ggplot2)
library(DT)

setwd("~/Desktop/STAA566/Assignment4")
ufo <- read.csv("complete.csv", header = T)
# grab only data points for the US
ufo <- ufo[ufo$country == "us",]
# remove data points without a state & duration
ufo <- ufo[!(is.na(ufo$state) | ufo$state ==""),]
ufo <- ufo[!(is.na(ufo$duration..seconds.) | ufo$duration..seconds. ==""),]
# convert state to upper case - for matching on US Map data later
ufo$abbr <- toupper(ufo$state)
# get data summarizations
ufoSummary <- ufo %>%
  group_by(abbr, year) %>%
  summarise(Total = n(), AvgDurSeconds = mean(duration..seconds.),
            AvgDurMinutes = mean(duration..seconds. / 60))
# get coordinates for the map
tmp_map <- map_data("state")
tmp_map$region <- str_to_title(tmp_map$region)
# create a state abbreviation column since thats what is in our data set
tmp_map$abbr <- usdata::state2abbr(tmp_map$region)
us_state_ufo <- merge(ufoSummary,tmp_map, by = "abbr")
# double check the lift join worked as expected
us_state_ufo %>% head(n=14)
   abbr year Total AvgDurSeconds AvgDurMinutes      long      lat group order
1    AL 1981     1             0             0 -87.46201 30.38968     1     1
2    AL 1981     1             0             0 -87.48493 30.37249     1     2
3    AL 1981     1             0             0 -87.52503 30.37249     1     3
4    AL 1981     1             0             0 -87.53076 30.33239     1     4
5    AL 1981     1             0             0 -87.57087 30.32665     1     5
6    AL 1981     1             0             0 -87.58806 30.32665     1     6
7    AL 1981     1             0             0 -87.59379 30.30947     1     7
8    AL 1981     1             0             0 -87.59379 30.28655     1     8
9    AL 1981     1             0             0 -87.67400 30.27509     1     9
10   AL 1981     1             0             0 -87.81152 30.25790     1    10
11   AL 1981     1             0             0 -87.88026 30.24644     1    11
12   AL 1981     1             0             0 -87.92037 30.24644     1    12
13   AL 1981     1             0             0 -87.95475 30.24644     1    13
14   AL 1981     1             0             0 -88.00632 30.24071     1    14
    region subregion
1  Alabama      <NA>
2  Alabama      <NA>
3  Alabama      <NA>
4  Alabama      <NA>
5  Alabama      <NA>
6  Alabama      <NA>
7  Alabama      <NA>
8  Alabama      <NA>
9  Alabama      <NA>
10 Alabama      <NA>
11 Alabama      <NA>
12 Alabama      <NA>
13 Alabama      <NA>
14 Alabama      <NA>

Create and display table

DT_table <- us_state_ufo %>%
  select(region, year, Total, AvgDurSeconds, AvgDurMinutes) %>%
  datatable(colnames = c("State", "Year", "Total\nSightings",
                         "Average Sightings\nIn Seconds","Average Sightings\nIn Minutes")) %>%
  formatRound(columns = c(3,4,5), 
              digits = 2)
DT_table

The echo: false option disables the printing of code (only output is displayed).